#include "logindialog.h"
#include <QFormLayout>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QPushButton>
#include <QLineEdit>
#include <QCheckBox>
#include <QMessageBox>
#include <qobject.h>

LoginDialog::LoginDialog(QWidget *parent)
    : QDialog(parent)
    , usernameLineEdit(new QLineEdit(this))
    , passwordLineEdit(new QLineEdit(this))
    , confirmPasswordLineEdit(new QLineEdit(this))
    , loginButton(new QPushButton("登录", this))
    , registerButton(new QPushButton("注册", this))
    , modeSwitchButton(new QPushButton("切换到注册", this))
    , statusLabel(new QLabel(this))
    , isRegisterMode(false)
{
    setupUI();
    setupConnections();
    setWindowTitle("登录");
    setFixedSize(300, 200);
    
    // 确保初始状态下登录按钮可见，注册按钮不可见
    loginButton->setVisible(true);
    registerButton->setVisible(false);
}

LoginDialog::~LoginDialog()
{
    // 析构函数
}

void LoginDialog::setupUI()
{
    // 设置密码输入框为密码模式
    passwordLineEdit->setEchoMode(QLineEdit::Password);
    confirmPasswordLineEdit->setEchoMode(QLineEdit::Password);
    
    // 默认隐藏确认密码输入框和注册按钮
    confirmPasswordLineEdit->setVisible(false);
    registerButton->setVisible(false);
    
    // 创建表单布局
    formLayout = new QFormLayout;
    formLayout->addRow("用户名:", usernameLineEdit);
    formLayout->addRow("密码:", passwordLineEdit);
    // 确认密码字段只在注册模式下添加到布局中
    
    // 创建按钮布局
    QHBoxLayout* buttonLayout = new QHBoxLayout;
    buttonLayout->addWidget(loginButton);
    buttonLayout->addWidget(registerButton);
    buttonLayout->addWidget(modeSwitchButton);
    
    // 创建主布局
    QVBoxLayout* mainLayout = new QVBoxLayout;
    mainLayout->addLayout(formLayout);
    mainLayout->addLayout(buttonLayout);
    mainLayout->addWidget(statusLabel);
    
    setLayout(mainLayout);
}

void LoginDialog::setupConnections()
{
    // 连接信号槽
    connect(loginButton, &QPushButton::clicked, this, &LoginDialog::onLoginClicked);
    connect(registerButton, &QPushButton::clicked, this, &LoginDialog::onRegisterClicked);
    connect(modeSwitchButton, &QPushButton::clicked, this, &LoginDialog::onModeChanged);
}

QString LoginDialog::getUsername() const
{
    return usernameLineEdit->text();
}

QString LoginDialog::getPassword() const
{
    return passwordLineEdit->text();
}

QString LoginDialog::getConfirmPassword() const
{
    return confirmPasswordLineEdit->text();
}

void LoginDialog::setLoginStatus(bool success, const QString& message)
{
    if (success) {
        statusLabel->setText("登录成功: " + message);
        statusLabel->setStyleSheet("color: green;");
        // 登录成功后关闭对话框
        accept();
    } else {
        statusLabel->setText("登录失败: " + message);
        statusLabel->setStyleSheet("color: red;");
    }
}

void LoginDialog::showError(const QString& error)
{
    statusLabel->setText("错误: " + error);
    statusLabel->setStyleSheet("color: red;");
}

void LoginDialog::onLoginClicked()
{
    // 处理登录按钮点击事件
    QString username = getUsername();
    QString password = getPassword();
    
    // 简单验证输入
    if (username.isEmpty() || password.isEmpty()) {
        showError("用户名和密码不能为空");
        return;
    }
    
    // 发出登录请求信号
    emit loginRequested(username, password);
}

void LoginDialog::onRegisterClicked()
{
    // 处理注册按钮点击事件
    QString username = getUsername();
    QString password = getPassword();
    QString confirmPassword = getConfirmPassword();
    
    // 简单验证输入
    if (username.isEmpty() || password.isEmpty() || confirmPassword.isEmpty()) {
        showError("所有字段都不能为空");
        return;
    }
    
    if (password != confirmPassword) {
        showError("密码和确认密码不匹配");
        return;
    }
    
    // 发出注册请求信号
    emit registerRequested(username, password);
}

void LoginDialog::onModeChanged()
{
    // 切换模式
    isRegisterMode = !isRegisterMode;
    
    if (isRegisterMode) {
        // 切换到注册模式
        setWindowTitle("注册");
        loginButton->setVisible(false);
        registerButton->setVisible(true);
        modeSwitchButton->setText("切换到登录");
        // 显示确认密码字段和标签
        confirmPasswordLineEdit->setVisible(true);
        // 添加确认密码字段到表单布局（如果尚未添加）
        if (formLayout->labelForField(confirmPasswordLineEdit) == nullptr) {
            formLayout->insertRow(2, "确认密码:", confirmPasswordLineEdit);
        } else {
            // 如果已经存在，确保标签可见
            QLabel* confirmLabel = qobject_cast<QLabel*>(formLayout->labelForField(confirmPasswordLineEdit));
            if (confirmLabel) {
                confirmLabel->setVisible(true);
            }
        }
    } else {
        // 切换到登录模式
        setWindowTitle("登录");
        loginButton->setVisible(true);
        registerButton->setVisible(false);
        modeSwitchButton->setText("切换到注册");
        // 隐藏确认密码字段和标签
        confirmPasswordLineEdit->setVisible(false);
        // 同时隐藏标签
        QLabel* confirmLabel = qobject_cast<QLabel*>(formLayout->labelForField(confirmPasswordLineEdit));
        if (confirmLabel) {
            confirmLabel->setVisible(false);
        }
    }
}

bool LoginDialog::validateInput() const
{
    // 验证用户输入
    QString username = getUsername();
    QString password = getPassword();
    
    if (username.isEmpty() || password.isEmpty()) {
        return false;
    }
    
    // 可以添加更多的验证规则
    return true;
}